#! /usr/bin/env python3

import smtplib
from email import encoders
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.base import MIMEBase
import keyring

pwd = keyring.get_password('yagmail', 'gomx1028@126.com')
email = MIMEMultipart()
target = ['1728148325@qq.com', 'gomx1028@163.com']
email['Subject'] = '带有附件的邮件'
email['From'] = 'gomx1028@126.com'
email['To'] = ','.join(target)

# 邮件正文
content = "长风破浪会有时，直挂云帆济沧海。\n春风得意马蹄疾，一日看尽长安花。\n年年岁岁花相似，岁岁年年人不同。"
att = MIMEText(content, 'plain', 'utf-8')
email.attach(att)

# 处理附件
att1 =MIMEBase('application', 'octet-stream')
att1.set_payload(open('小岛.jpg','rb').read())
att1.add_header('Content-Disposition', 'attachment', filename=Header('小岛.jpg', 'utf-8').encode())
encoders.encode_base64(att1)
email.attach(att1)

# 发送邮件
smtp = smtplib.SMTP('smtp.126.com', port=25)
smtp.login(user='gomx1028@126.com', password=pwd)
smtp.sendmail('gomx1028@126.com', ','.join(target), email.as_string())
smtp.close()
print("发送成功")
